var express=require('express')
// 先引入第三方包
var app=express()
//实例化express

// 设置静态资源托管文件夹
app.use(express.static('static'))

// 设置接口前。加上处理post请求参数 的中间件，不然直接请求post回来的是underfind
app.use( express.json() );
app.use( express.urlencoded({extended:false}) );

//先暂时全局定义一个返回客户端的数据
var arr=[
    {email:'1772574931@qq.com',name:'沈健',phone:'13611129071',pass:'1234',ingroup:'编辑组',isadmin:true,status:true
    },
    {email:'1172574931@qq.com',name:'小马',phone:'13611129072',pass:'1235',ingroup:'编辑组',isadmin:true,status:true}
    , {email:'1772574931@qq.com',name:'老朱',phone:'13611129073',pass:'1236',ingroup:'编辑组',isadmin:true,status:true}
    , {email:'1772574931@qq.com',name:'阿宝',phone:'13611129074',pass:'1237',ingroup:'编辑组',isadmin:true,status:true
},
{email:'1772574931@qq.com',name:'星渊',phone:'13611129072',pass:'1238',ingroup:'编辑组',isadmin:true,status:true}
, {email:'17888m4773@qq.co',name:'兴隆',phone:'13611129073',pass:'1239',ingroup:'编辑组',isadmin:true,status:true}
,{email:'178884773@qq.com',name:'立立',phone:'13611129071',pass:'12310',ingroup:'编辑组',isadmin:true,status:true
},
{email:'178884773@qq.com',name:'松松娃',phone:'13611129072',pass:'12311',ingroup:'编辑组',isadmin:true,status:true}
, {email:'178884773@qq.com',name:'奇想',phone:'13611129073',pass:'12312',ingroup:'编辑组',isadmin:true,status:true},
{email:'178884773@qq.com',name:'杨吴龙',phone:'13611129071',pass:'12313',ingroup:'编辑组',isadmin:true,status:true
},
{email:'178884773@qq.com',name:'孙瑞',phone:'13611129072',pass:'12314',ingroup:'编辑组',isadmin:true,status:true}
, {email:'178884773@qq.com',name:'丁施范',phone:'13611129073',pass:'12315',ingroup:'编辑组',isadmin:true,status:true}
, {email:'17888m4773@qq.co',name:'兴隆',phone:'13611129073',pass:'1239',ingroup:'编辑组',isadmin:true,status:true}
,{email:'178884773@qq.com',name:'立立',phone:'13611129071',pass:'12310',ingroup:'编辑组',isadmin:true,status:true
},
{email:'178884773@qq.com',name:'松松娃',phone:'13611129072',pass:'12311',ingroup:'编辑组',isadmin:true,status:true}
, {email:'178884773@qq.com',name:'奇想',phone:'13611129073',pass:'12312',ingroup:'编辑组',isadmin:true,status:true},
{email:'178884773@qq.com',name:'杨吴龙',phone:'13611129071',pass:'12313',ingroup:'编辑组',isadmin:true,status:true
},
{email:'178884773@qq.com',name:'孙瑞',phone:'13611129072',pass:'12314',ingroup:'编辑组',isadmin:true,status:true}
, {email:'178884773@qq.com',name:'丁施范',phone:'13611129073',pass:'12315',ingroup:'编辑组',isadmin:true,status:true}
, {email:'178884773@qq.com',name:'丁施范',phone:'13611129073',pass:'12315',ingroup:'编辑组',isadmin:true,status:true}
]
//设置接口
//分页查询
app.get('/user/list',(req,res)=>{
    var {page,count}=req.query;
    var newarr=arr.slice((page-1)*count,page*count);
    res.send({code:200,msg:'恭喜你，数据回来啦',list:newarr})
})

//模糊搜索
app.get("/user/search",(req,res)=>{
    var {searchkey}=req.query;
    var newarr=arr.filter((item)=>{
        return new RegExp(searchkey).test(item.name)
    })
    res.send({code:200,msg:'查询到啦！',list:newarr})
})

// 登录
app.post("/user/login",(req,res)=>{
    var {phone,pass}=req.body;
    var i=arr.findIndex((item,index)=>{
        return item.phone==phone&&item.pass==pass;
    })
    if(i!=-1){
        res.send({code:200,msg:'登陆成功',userinfo:arr[i],token:''});
    }else{
        res.send({code:400,msg:'登陆失败'})
    }
})



app.listen(3060,()=>{
    console.log('服务端运行在: http://localhost:3060');
})
